<?php

/**
 * @author Jeremy
 * @version 1.0
 */
class Stage
{

    /**
     * @var int
     */
    protected $Id;
    /**
     * @var int
     */
    protected $IdEtudiant;
    /**
     * @var int
     */
    protected $IdTuteurEntreprise;
    /**
     * @var Attribution
     */
    protected $Attribution;
    /**
     * @var int
     */
    protected $IdTuteurEnseignant;
    /**
     * @var int
     */
    protected $IdPromo;
    /**
     * @var int
     */
    protected $IdOffre;
    /**
     * @var string
     */
    protected $Nom;
    /**
     * @var string
     */
    protected $Description;
    /**
     * @var int
     */
    protected $DateDebut;
    /**
     * @var int
     */
    protected $DateFin;
    /**
     * @var int
     */
    protected $DateCreationFiche;
    /**
     * @var int
     */
    protected $DateCreationConvention;
    /**
     * @var int
     */
    protected $SignatureUFR;
    /**
     * @var int
     */
    protected $SignatureResponsable;
    /**
     * @var int
     */
    protected $SignatureEntreprise;
    /**
     * @var int
     */
    protected $SignatureEtudiant;
    /**
     * @var int
     */
    protected $SignatureRelationInternational;
    /**
     * @var int
     */
    protected $Revision;
    /**
     * @var string
     */
    protected $EtatFiche;

    /**
     * Constructeur
     * @param int $idS  
     */
    public function __construct($idS = 0)
    {

        // Traitement
        if (empty($idS))
        {
            // ID
            $this->IdEtudiant = 0;
            $this->IdTuteurEntreprise = 0;
            $this->IdPromo = 0;
            $this->IdOffre = 0;

            // STRING
            $this->Nom = ""; // VARCHAR(11)
            $this->Description = ""; // TEXT
            // DATE (INT)
            $this->DateDebut = 0;
            $this->DateFin = 0;
            $this->DateCreationFiche = 0;
            $this->DateCreationConvention = 0;
            $this->SignatureUFR = 0;
            $this->SignatureResponsable = 0;
            $this->SignatureEntreprise = 0;
            $this->SignatureEtudiant = 0;
            $this->SignatureRelationInternational = 0;

            // INT
            $this->Revision = 0;

            // ENUM
            $this->EtatFiche = "";
            
        }
        else
        {
            $sql = SQL::getInstance()->getConnection();
            //On retourne l'entree demandee
            $query = 'SELECT Stage.* FROM Stage WHERE Stage.IdStage = :id';
            $c = $sql->prepare($query);
            $c->bindParam(':id', $idS, PDO::PARAM_INT);

            // Execution de la requete retourne false si echec
            if ($c->execute())
            {
                $result = $c->fetch(PDO::FETCH_OBJ);
                if (!empty($result))
                {
                    // ID
                    $this->Id = $result->IdStage;
                    $this->IdEtudiant = $result->IdEtudiant;
                    $this->IdTuteurEntreprise = Attribution::getIdTuteurEntrepriseActuelByStage($this->Id);
                    $this->IdTuteurEnseignant = $result->IdTuteurEnseignant;
                    $this->IdPromo = $result->IdPromo;
                    $this->IdOffre = $result->IdOffre;

                    // STRING
                    $this->Nom = $result->TitreStage; // VARCHAR(11)
                    $this->Description = $result->DescriptionStage; // TEXT
                    // DATE (INT)
                    $this->DateDebut = $result->DateDebutStage;
                    $this->DateFin = $result->DateFinStage;
                    $this->DateCreationFiche = $result->DateCreationFiche;
                    $this->DateCreationConvention = $result->DateCreationConvention;
                    $this->SignatureUFR = $result->DateSignatureUFR;
                    $this->SignatureResponsable = $result->DateSignatureResponsable;
                    $this->SignatureEntreprise = $result->DateSignatureEntreprise;
                    $this->SignatureEtudiant = $result->DateSignatureEtudiant;
                    $this->SignatureRelationInternational = $result->DateSignatureRelationInternational;

                    // INT
                    $this->Revision = $result->Revision;

                    // ENUM
                    $this->EtatFiche = $result->EtatFicheStage;

                    // ATTRIBUTION
                    $this->Attribution = new Attribution(Attribution::getIdAttributionActuelByStage($this->Id));

                    // Gestion erreur requete
                    $this->requeteOk = true;
                    $this->errormsg = "Stage trouve";
                }
                else
                {
                    $this->requeteOk = false;
                    $this->errormsg = "Ce stage n'existe pas !";
                }
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
            }
        }
    }

    /**
     * Ajout Bdd
     */
    public function insertStage()
    {
        $sql = SQL::getInstance()->getConnection();
        $query =
                'INSERT INTO Stage
		(
			IdEtudiant,
			IdTuteurEnseignant,
			IdTuteurEntreprise,
			IdPromo,
			IdOffre,
			TitreStage,
			DescriptionStage,
			DateDebutStage,
			DateFinStage,
			DateCreationFiche,
			DateCreationConvention,
			DateSignatureUFR,
			DateSignatureResponsable,
			DateSignatureEntreprise,
			DateSignatureEtudiant,
			DateSignatureRelationInternational,
			Revision,
			EtatFicheStage
		)
		VALUES
		(
			:idE,
			:idTE,
                        :idTC,
			:idP,
			:idO,
			:nom,
			:description,
			:dateD,
			:dateF,
			:dateCF,
			:dateCC,
			:signatureUFR,
			:signatureR,
			:signatureEnt,
			:signatureEtu,
			:signatureRI,
			:revision,
			:etat
		)';
        $sql->beginTransaction();
        try
        {
            $c = $sql->prepare($query);

            $c->bindParam(':idE', $this->IdEtudiant, PDO::PARAM_INT);
            $c->bindParam(':idTE', $this->IdTuteurEnseignant, PDO::PARAM_INT);
            $c->bindParam(':idTC', $this->IdTuteurEntreprise, PDO::PARAM_INT);
            $c->bindParam(':idP', $this->IdPromo, PDO::PARAM_INT);
            $c->bindParam(':idO', $this->IdOffre, PDO::PARAM_INT);

            $c->bindParam(':nom', $this->Nom, PDO::PARAM_STR);
            $c->bindParam(':description', $this->Description, PDO::PARAM_STR);

            $c->bindParam(':dateD', $this->DateDebut, PDO::PARAM_INT);
            $c->bindParam(':dateF', $this->DateFin, PDO::PARAM_INT);
            $c->bindParam(':dateCF', $this->DateCreationFiche, PDO::PARAM_INT);
            $c->bindParam(':dateCC', $this->DateCreationConvention, PDO::PARAM_INT);
            $c->bindParam(':signatureUFR', $this->SignatureUFR, PDO::PARAM_INT);
            $c->bindParam(':signatureR', $this->SignatureResponsable, PDO::PARAM_INT);
            $c->bindParam(':signatureEnt', $this->SignatureEntreprise, PDO::PARAM_INT);
            $c->bindParam(':signatureEtu', $this->SignatureEtudiant, PDO::PARAM_INT);
            $c->bindParam(':signatureRI', $this->SignatureRelationInternational, PDO::PARAM_INT);

            $c->bindParam(':revision', $this->Revision, PDO::PARAM_INT);

            $c->bindParam(':etat', $this->EtatFiche, PDO::PARAM_STR);

            if ($c->execute())
            {
                $this->Id = $sql->lastInsertId();
                $sql->commit();
                $c->closeCursor();
                if (!empty($this->Attribution))
                {
                    $this->Attribution->setIdStage($this->Id);
                    $this->Attribution->insertAttribution();
                }
                $this->requeteOk = true;
                $this->errormsg = "";
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
                $this->IdUtilisateur = 0;
            }
        }
        catch (PDOException $Exception)
        {
            $this->requeteOk = false;
            $this->errormsg = $c->errorInfo();
            $sql->rollback();
            $c->closeCursor();
            $this->Id = 0;
        }
    }

    /**
     * Update Bdd
     */
    public function updateStage()
    {
        $sql = SQL::getInstance()->getConnection();
        $query =
                'UPDATE Stage
		SET 
			IdEtudiant							= :idE,
			IdTuteurEnseignant					= :idTE,
			IdPromo								= :idP,
			IdOffre								= :idO,
			TitreStage							= :nom,
			DescriptionStage					= :description,
			DateDebutStage						= :dateD,
			DateFinStage						= :dateF,
			DateCreationFiche					= :dateCF,
			DateCreationConvention				= :dateCC,
			DateSignatureUFR					= :signatureUFR,
			DateSignatureResponsable			= :signatureR,
			DateSignatureEntreprise				= :signatureEnt,
			DateSignatureEtudiant				= :signatureEtu,
			DateSignatureRelationInternational	= :signatureRI,
			Revision							= :revision,
			EtatFicheStage							= :etat
		WHERE 
			IdStage								= :id';
        $sql->beginTransaction();
        try
        {
            $c = $sql->prepare($query);

            $c->bindParam(':id', $this->Id, PDO::PARAM_INT);
            $c->bindParam(':idE', $this->IdEtudiant, PDO::PARAM_INT);
            $c->bindParam(':idTE', $this->IdTuteurEnseignant, PDO::PARAM_INT);
            $c->bindParam(':idP', $this->IdPromo, PDO::PARAM_INT);
            $c->bindParam(':idO', $this->IdOffre, PDO::PARAM_INT);

            $c->bindParam(':nom', $this->Nom, PDO::PARAM_STR);
            $c->bindParam(':description', $this->Description, PDO::PARAM_STR);

            $c->bindParam(':dateD', $this->DateDebut, PDO::PARAM_INT);
            $c->bindParam(':dateF', $this->DateFin, PDO::PARAM_INT);
            $c->bindParam(':dateCF', $this->DateCreationFiche, PDO::PARAM_INT);
            $c->bindParam(':dateCC', $this->DateCreationConvention, PDO::PARAM_INT);
            $c->bindParam(':signatureR', $this->SignatureResponsable, PDO::PARAM_INT);
            $c->bindParam(':signatureEnt', $this->SignatureEntreprise, PDO::PARAM_INT);
            $c->bindParam(':signatureEtu', $this->SignatureEtudiant, PDO::PARAM_INT);
            $c->bindParam(':signatureUFR', $this->SignatureUFR, PDO::PARAM_INT);
            $c->bindParam(':signatureRI', $this->SignatureRelationInternational, PDO::PARAM_INT);

            $c->bindParam(':revision', $this->Revision, PDO::PARAM_INT);

            $c->bindParam(':etat', $this->EtatFiche, PDO::PARAM_STR);



            if ($c->execute())
            {
                $this->requeteOk = true;
                $this->errormsg = "";
                $sql->commit();
                $c->closeCursor();
                if( !empty($this->Attribution) )
                {
                    $attr = new Attribution($this->Attribution->getIdAttribution());
                    if( !$attr->requeteOk )
                    {
                        $this->Attribution->insertAttribution();
                    }
                }
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
            }
        }
        catch (PDOException $Exception)
        {
            $this->requeteOk = false;
            $this->errormsg = $c->errorInfo();
            $sql->rollback();
            $c->closeCursor();
        }
    }

    /**
     * @static
     * @return array Array de Fiche de stage valide
     */
    static public function getListValide($limit=0, $begin=0)
    {
        if (empty($limit))
        {
            $sql = SQL::getInstance()->getConnection();
            $query = "SELECT *
                                FROM Stage
                                WHERE EtatFicheStage='acceptee'";
            $c = $sql->query($query);
            $result = $c->fetchAll(PDO::FETCH_OBJ);
            return $result;
        }
        else
        {
            $sql = SQL::getInstance()->getConnection();
            $query = "SELECT *
                                FROM Stage
                                WHERE EtatFicheStage='acceptee'";
            $query .= ' LIMIT :begin,:limit';
            $c = $sql->prepare($query);
            $c->bindParam(':begin', $begin, PDO::PARAM_INT);
            $c->bindParam(':limit', $limit, PDO::PARAM_INT);
            if ($c->execute())
            {
                $result = $c->fetchAll(PDO::FETCH_OBJ);
                return $result;
            }
            else
            {
                return array();
            }
        }
    }

    /**
     * @static
     * @return array Fiche de stage en attente
     */
    static public function getListAttente($limit=0, $begin=0)
    {
        if (empty($limit))
        {
            $sql = SQL::getInstance()->getConnection();
            $query = "SELECT s.* , e.NomEntreprise
                            FROM Stage s
                            INNER JOIN ContactEntreprise c ON c.idContactEntreprise = s.IdTuteurEntreprise
                            INNER JOIN Entreprise e ON e.IdEntreprise = c.IdEntreprise
                            WHERE s.EtatFicheStage='en attente'";
            $c = $sql->query($query);
            $result = $c->fetchAll(PDO::FETCH_OBJ);
            return $result;
        }
        else
        {
            $sql = SQL::getInstance()->getConnection();
            $query = "SELECT s.* , e.NomEntreprise
                            FROM Stage s
                            INNER JOIN ContactEntreprise c ON c.idContactEntreprise = s.IdTuteurEntreprise
                            INNER JOIN Entreprise e ON e.IdEntreprise = c.IdEntreprise
                            WHERE s.EtatFicheStage='en attente'";
            $query .= ' LIMIT :begin,:limit';
            $c = $sql->prepare($query);
            $c->bindParam(':begin', $begin, PDO::PARAM_INT);
            $c->bindParam(':limit', $limit, PDO::PARAM_INT);
            if ($c->execute())
            {
                $result = $c->fetchAll(PDO::FETCH_OBJ);
                return $result;
            }
            else
            {
                return array();
            }
        }
    }

    /**
     * @static
     * @return array Array de Stage
     */
    static public function getList()
    {
        $sql = SQL::getInstance()->getConnection();

        $query = "	SELECT *
				FROM Stage
				";
        $c = $sql->query($query);
        $result = $c->fetchAll(PDO::FETCH_OBJ);

        return $result;
    }

    /**
     * @static
     * @return int Nombre de fiche en attente
     */
    static public function attenteCount()
    {
        $sql = SQL::getInstance()->getConnection();

        $query = "SELECT COUNT(IdStage) as count FROM Stage WHERE EtatFicheStage='en attente'";
        $c = $sql->query($query);
        $result = $c->fetch(PDO::FETCH_OBJ);
        return $result->count;
    }

    /**
     * @static
     * @return int nombre de fiches acceptees devenues conventions
     */
    static public function accepteeCount()
    {
        $sql = SQL::getInstance()->getConnection();

        $query = "SELECT COUNT(IdStage) as count FROM Stage WHERE EtatFicheStage='acceptee'";
        $c = $sql->query($query);
        $result = $c->fetch(PDO::FETCH_OBJ);
        return $result->count;
    }

    /**
     * @static
     * @return int nombre de stage fiches et conventions confondues
     */
    static public function globalCount()
    {
        $sql = SQL::getInstance()->getConnection();

        $query = "SELECT COUNT(IdStage) as count FROM Stage";
        $c = $sql->query($query);
        $result = $c->fetch(PDO::FETCH_OBJ);
        return $result->count;
    }
    /**
     * @static
     * @return int
     */
    static public function getGlobalNbrConvention()
    {
        $sql = SQL::getInstance()->getConnection();

        $query = "SELECT COUNT(IdStage) as count FROM Stage WHERE EtatFicheStage = 'acceptee' AND ( DateCreationConvention IS NOT NULL OR DateCreationConvention != 0)";
        $c = $sql->query($query);
        $result = $c->fetch(PDO::FETCH_OBJ);
        return $result->count;
    }

    /**
     *
     * @return int
     */
    public function getIdEntreprise()
    {
        $sql = SQL::getInstance()->getConnection();

        $query = "  SELECT s.* , e.IdEntreprise
                            FROM Stage s
                            INNER JOIN ContactEntreprise c ON c.idContactEntreprise = s.IdTuteurEntreprise
                            INNER JOIN Entreprise e ON e.IdEntreprise = c.IdEntreprise
                            WHERE s.IdStage = :id
                        ";

        $c = $sql->prepare($query);
        $c->bindParam(":id", $this->Id, PDO::PARAM_INT);
        $c->execute();
        $result = $c->fetch(PDO::FETCH_OBJ);
        return $result->IdEntreprise;
    }

    /**
     *
     * @return int
     */
    public function getIdStage()
    {
        return $this->Id;
    }

    /**
     *
     * @return int
     */
    public function getIdEtudiant()
    {
        return $this->IdEtudiant;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setIdEtudiant($value)
    {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($value) && $value > 0)
        {
            $this->IdEtudiant = $value;
        }
        else
        {
            if (!isset($value))
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'id etudiant ne peut etre null !";
            }
            if (isset($value) && $value <= 0)
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'id etudiant ne peut etre <= 0 !";
            }
            else
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Cet id etudiant n'est pas valide !";
            }

            $this->IdEtudiant = 0;
        }
        return $errTab;
    }

    /**
     *
     * @return int
     */
    public function getIdTuteurEntreprise()
    {
        return $this->IdTuteurEntreprise;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setIdTuteurEntreprise($value)
    {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($value) && $value > 0)
        {
            // -----------------------------------------------------------------------------------------
			$this->IdTuteurEntreprise = $value;
			
            $this->Attribution = new Attribution();
            $this->Attribution->setIdTuteurEntreprise($value);
            $this->Attribution->setIdStage($this->Id);
            $this->Attribution->setDateAjout();
			
        }
        else
        {
            if (!isset($value))
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'id tuteur entreprise ne peut etre null !";
            }
            if (isset($value) && $value <= 0)
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'id tuteur entreprise ne peut etre <= 0 !";
            }
            else
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Cet id tuteur entreprise n'est pas valide !";
            }
        }
        return $errTab;
    }

    /**
     *
     * @return int
     */
    public function getIdPromo()
    {
        return $this->IdPromo;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setIdPromo($value)
    {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($value) && $value > 0)
        {
            $this->IdPromo = $value;
        }
        else
        {
            if (!isset($value))
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'id promo ne peut etre null !";
            }
            if (isset($value) && $value <= 0)
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'id promo ne peut etre <= 0 !";
            }
            else
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Cet id promo n'est pas valide !";
            }

            $this->IdPromo = 0;
        }
        return $errTab;
    }

    /**
     *
     * @return int
     */
    public function getIdOffre()
    {
        return $this->IdOffre;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setIdOffre($value)
    {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($value) && $value > 0)
        {
            $this->IdOffre = $value;
        }
        else
        {
            if (!isset($value))
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'id offre ne peut etre null !";
            }
            if (isset($value) && $value <= 0)
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'id offre ne peut etre <= 0 !";
            }
            else
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Cet id offre n'est pas valide !";
            }

            $this->IdOffre = 0;
        }
        return $errTab;
    }

    /**
     *
     * @return int
     */
    public function getIdTuteurEnseignant()
    {
        return $this->IdTuteurEnseignant;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setIdTuteurEnseignant($value)
    {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($value) && $value > 0)
        {
            $this->IdTuteurEnseignant = $value;
        }
        else
        {
            if (!isset($value))
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'id ne peut etre null !";
            }
            if (isset($value) && $value <= 0)
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'id ne peut etre <= 0 !";
            }
            else
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Cet id n'est pas valide !";
            }

            $this->IdOffre = 0;
        }
        return $errTab;
    }

    /**
     *
     * @return string
     */
    public function getTitreStage()
    {
        return stripcslashes($this->Nom);
    }

    /**
     * @param string $value
     * @return array
     */
    public function setTitreStage($value)
    {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($value) && $value != "" && strlen($value) <= 255)
        {
            $this->Nom = htmlentities($value);
        }
        else
        {
            if (!isset($value))
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Le nom ne peut etre null !";
            }
            if (isset($value) && $value == "")
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Le nom ne peut etre vide !";
            }
            else
            {
                if (isset($value) && strlen($value) > 255)
                {
                    $errTab["boolOk"] = false;
                    $errTab["errmsg"] = "Le nom doit etre compris entre 1 et 255 caracteres !";
                }
                else
                {
                    $errTab["boolOk"] = false;
                    $errTab["errmsg"] = "Ce nom n'est pas valide !";
                }
            }

            $this->Nom = "";
        }
        return $errTab;
    }

    /**
     *
     * @return string
     */
    public function getDescriptionStage()
    {
        return stripcslashes($this->Description);
    }

    /**
     * @param string $value
     * @return array
     */
    public function setDescriptionStage($value)
    {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($value) && $value != "")
        {
            $this->Description = htmlentities($value);
        }
        else
        {
            if (!isset($value))
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "La description ne peut etre null !";
            }
            if (isset($value) && $value == "")
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "La description ne peut etre vide !";
            }
            else
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Cette description n'est pas valide !";
            }

            $this->Description = "";
        }
        return $errTab;
    }

    /**
     *
     * @return int
     */
    public function getDateDebutStage()
    {
        return $this->DateDebut;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setDateDebutStage($value)
    {
        if (isset($value) && strlen($value) == 10)
        {
            $this->DateDebut = $value;
        }
        else
        {
            $errTab["boolOk"] = false;
            $errTab["errmsg"] = "Cette date n'est pas valide !";

            $this->DateDebut = 0;
        }
    }

    /**
     *
     * @return int
     */
    public function getDateFinStage()
    {
        return $this->DateFin;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setDateFinStage($value)
    {
        if (isset($value) && strlen($value) == 10)
        {
            $this->DateFin = $value;
        }
        else
        {
            $errTab["boolOk"] = false;
            $errTab["errmsg"] = "Cette date n'est pas valide !";

            $this->DateFin = 0;
        }
    }

    /**
     *
     * @return int
     */
    public function getDateCreationFicheStage()
    {
        return $this->DateCreationFiche;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setDateCreationFicheStage()
    {
        $this->DateCreationFiche = time();
    }

    /**
     *
     * @return int
     */
    public function getDateCreationConventionStage()
    {
        return $this->DateCreationConvention;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setDateCreationConventionStage()
    {
        $this->DateCreationConvention = time();
    }

    /**
     *
     * @return int
     */
    public function getSignatureUFR()
    {
        return $this->SignatureUFR;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setSignatureUFR($value)
    {
        if (isset($value) && strlen($value) == 10)
        {
            $this->SignatureUFR = $value;
        }
        else
        {
            $errTab["boolOk"] = false;
            $errTab["errmsg"] = "Cette date n'est pas valide !";

            $this->SignatureUFR = 0;
        }
    }

    /**
     *
     * @return int
     */
    public function getSignatureResponsable()
    {
        return $this->SignatureResponsable;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setSignatureResponsable($value)
    {
        if (isset($value) && strlen($value) == 10)
        {
            $this->SignatureResponsable = $value;
        }
        else
        {
            $errTab["boolOk"] = false;
            $errTab["errmsg"] = "Cette date n'est pas valide !";

            $this->SignatureResponsable = 0;
        }
    }

    /**
     *
     * @return int
     */
    public function getSignatureEntreprise()
    {
        return $this->SignatureEntreprise;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setSignatureEntreprise($value)
    {
        if (isset($value) && strlen($value) == 10)
        {
            $this->SignatureEntreprise = $value;
        }
        else
        {
            $errTab["boolOk"] = false;
            $errTab["errmsg"] = "Cette date n'est pas valide !";

            $this->SignatureEntreprise = 0;
        }
    }

    /**
     *
     * @return int
     */
    public function getSignatureEtudiant()
    {
        return $this->SignatureEtudiant;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setSignatureEtudiant($value)
    {
        if (isset($value) && strlen($value) == 10)
        {
            $this->SignatureEtudiant = $value;
        }
        else
        {
            $errTab["boolOk"] = false;
            $errTab["errmsg"] = "Cette date n'est pas valide !";

            $this->SignatureEtudiant = 0;
        }
    }

    /**
     *
     * @return int
     */
    public function getSignatureRelationInternational()
    {
        return $this->SignatureRelationInternational;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setSignatureRelationInternational($value)
    {
        if (isset($value) && strlen($value) == 10)
        {
            $this->SignatureRelationInternational = $value;
        }
        else
        {
            $errTab["boolOk"] = false;
            $errTab["errmsg"] = "Cette date n'est pas valide !";

            $this->SignatureRelationInternational = 0;
        }
    }

    /**
     *
     * @return int
     */
    public function getRevision()
    {
        return $this->Revision;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setRevision($value)
    {
        if (isset($value) && !empty($value))
        {
            $this->Revision = $value;
        }
        else
        {
            $errTab["boolOk"] = false;
            $errTab["errmsg"] = "Cette revision n'est pas valide !";

            $this->Revision = 0;
        }
    }

    /**
     *
     * @return string
     */
    public function getEtatFiche()
    {
        return $this->EtatFiche;
    }

    /**
     * @param string $value
     * @return array
     */
    public function setEtatFiche($value)
    {
        if (isset($value) && !empty($value))
        {
            $this->EtatFiche = $value;
        }
        else
        {
            $errTab["boolOk"] = false;
            $errTab["errmsg"] = "Cet etat n'est pas valide !";

            $this->EtatFiche = "";
        }
    }

    /**
     * @static
     * @param int $limit
     * @param int $begin
     * @return array Array de Conventions de stage
     */
    static public function getListConventions($limit=0, $begin=0)
    {
        if (empty($limit))
        {
            $sql = SQL::getInstance()->getConnection();
            $query = 'SELECT s.*, u.NomUilisateur as NomEtudiant, u.PrenomUtilisateur as PrenomEtudiant, p.NomPromo
                FROM Stage s
                INNER JOIN Utilisateur u ON s.IdEtudiant = u.IdUtilisateur
                INNER JOIN Etudiant e ON s.IdEtudiant = e.IdEtudiant
                INNER JOIN Promo p ON e.IdPromo = p.IdPromo
                WHERE s.EtatFicheStage="acceptee"';
            $c = $sql->query($query);
            $result = $c->fetchAll(PDO::FETCH_OBJ);
            return $result;
        }
        else
        {
            $sql = SQL::getInstance()->getConnection();
            $query = 'SELECT s.*, u.NomUtilisateur as NomEtudiant, u.PrenomUtilisateur as PrenomEtudiant, p.NomPromo
                FROM Stage s
                INNER JOIN Utilisateur u ON s.IdEtudiant = u.IdUtilisateur
                INNER JOIN Etudiant e ON s.IdEtudiant = e.IdEtudiant
                INNER JOIN Promo p ON e.IdPromo = p.IdPromo
                WHERE s.EtatFicheStage="acceptee"';
            $query .= ' LIMIT :begin,:limit';
            $c = $sql->prepare($query);
            $c->bindParam(':begin', $begin, PDO::PARAM_INT);
            $c->bindParam(':limit', $limit, PDO::PARAM_INT);
            if ($c->execute())
            {
                $result = $c->fetchAll(PDO::FETCH_OBJ);
                return $result;
            }
            else
            {
                return array();
            }
        }
    }

    /**
     * @static
     * @param int $id Identifiant d'un etudiant
     * @return array Array de conventions de stage
     */
    static public function getStagesByIdEtudiant($id)
    {
        if (isset($id) && !empty($id))
        {
            $sql = SQL::getInstance()->getConnection();
            $query = "SELECT s.* , e.NomEntreprise
                          FROM Stage s
                          INNER JOIN ContactEntreprise c ON c.idContactEntreprise = s.IdTuteurEntreprise
                          INNER JOIN Entreprise e ON e.IdEntreprise = c.IdEntreprise
                          WHERE s.IdEtudiant = :id
                            AND (s.DateCreationConvention > 0 AND s.DateCreationConvention IS NOT NULL)";
            $c = $sql->prepare($query);
            $c->bindParam(":id", $id, PDO::PARAM_INT);
            $c->execute();
            $result = $c->fetchAll(PDO::FETCH_OBJ);
            return $result;
        }
    }
	
	

    /**
     * @static
     * @param int $id
     * @return array Array de fiche de stage
     */
    static public function getFichesByIdEtudiant($id)
    {
        if (isset($id) && !empty($id))
        {
            $sql = SQL::getInstance()->getConnection();
            $query = "SELECT s.* , e.NomEntreprise
                          FROM Stage s
                          INNER JOIN ContactEntreprise c ON c.idContactEntreprise = s.IdTuteurEntreprise
                          INNER JOIN Entreprise e ON e.IdEntreprise = c.IdEntreprise
                          WHERE s.IdEtudiant = :id
                            AND (s.DateCreationConvention is NULL OR s.DateCreationConvention = 0)";
            $c = $sql->prepare($query);
            $c->bindParam(":id", $id, PDO::PARAM_INT);
            $c->execute();
            $result = $c->fetchAll(PDO::FETCH_OBJ);
            return $result;
        }
    }

    /**
     * @static
     * @param int $id Identifiant d'un enseignant
     * @return array Array de stage
     */
    static public function getCurrentStagesByIdEnseignant($id)
    {
        if (isset($id) && !empty($id))
        {
            $sql = SQL::getInstance()->getConnection();
            $query = "SELECT s.*
                        FROM Stage s
                        INNER JOIN Enseignant e ON e.idEnseignant = s.IdTuteurEnseignant
                        WHERE s.IdTuteurEnseignant = :id
                        AND (s.DateCreationConvention > 0)
                        AND (s.DateFinStage > :time)
                        ";
            $c = $sql->prepare($query);
            $c->bindParam(":id", $id, PDO::PARAM_INT);
            $c->bindParam(":time", time(), PDO::PARAM_INT);
            $c->execute();
            $result = $c->fetchAll(PDO::FETCH_OBJ);
            return $result;
        }
    }

    /**
     * @static
     * @param int $id Identifiant d'un enseignant
     * @return array Array de stage
     */
    static public function getPastStagesByIdEnseignant($id)
    {
        if (isset($id) && !empty($id))
        {
            $sql = SQL::getInstance()->getConnection();
            $query = "SELECT s.*
                        FROM Stage s
                        INNER JOIN Enseignant e ON e.idEnseignant = s.IdTuteurEnseignant
                        WHERE s.IdTuteurEnseignant = :id
                        AND (s.DateCreationConvention > 0)
                        AND (s.DateFinStage < :time)
                        ";
            $c = $sql->prepare($query);
            $c->bindParam(":id", $id, PDO::PARAM_INT);
            $c->bindParam(":time", time(), PDO::PARAM_INT);
            $c->execute();
            $result = $c->fetchAll(PDO::FETCH_OBJ);
            return $result;
        }
    }
    
    /*
     * @static
     * @param int Id du contact Entreprise
     * @return array Array de stage
     */
    static public function getStagesByIdContact($id)
    {
        if (isset($id) && !empty($id))
        {
            $sql = SQL::getInstance()->getConnection();
            $query = "SELECT s.*
                        FROM Stage s
                        WHERE s.IdTuteurEntreprise = :id
                        ";
            $c = $sql->prepare($query);
            $c->bindParam(":id", $id, PDO::PARAM_INT);
            $c->execute();
            $result = $c->fetchAll(PDO::FETCH_OBJ);
            return $result;
        }
    }
}

?>